package com.google.project.client.HumanResources;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.project.client.Service.ProjectService;
import com.google.project.client.Service.ProjectServiceAsync;
import com.google.project.client.Service.ProjectStaffService;
import com.google.project.client.Service.ProjectStaffServiceAsync;
import com.google.project.client.Service.SpecializeService;
import com.google.project.client.Service.SpecializeServiceAsync;
import com.google.project.client.Service.UsersService;
import com.google.project.client.Service.UsersServiceAsync;
import com.google.project.shared.CommonFuntion;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.ProjectStaffDTO;
import com.google.project.shared.SpecializeDTO;
import com.google.project.shared.TaskRequireDTO;
import com.google.project.shared.UsersDTO;
import com.google.gwt.user.client.ui.Label;

public class HMStaffForAddProject extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField(provided = true)
	CellTable<UsersDTO> ctbStaffList = new CellTable<UsersDTO>();
	@UiField
	SimplePager pagerListSearch;
	@UiField
	Button btnMove;
	@UiField
	Button btnRemove;
	@UiField
	Button btnSearch;
	@UiField
	ListBox cbSpecialize;
	@UiField
	TextBox txtName;
	@UiField
	TextBox txtCode;
	@UiField
	Button btnCancel;
	@UiField
	Button btnAdd;
	@UiField(provided = true)
	CellTable<UsersDTO> ctbResult = new CellTable<UsersDTO>();
	@UiField
	SimplePager pagerAddList;
	@UiField
	ListBox cboProject;
	@UiField
	ListBox cbExperient;
	@UiField
	DateBox txtFromDate;
	@UiField
	DateBox txtToDate;
	@UiField
	Label lblNumOfEmp;
	@UiField
	Label lblNumEmp;
	@UiField
	Label lblNumOfResponse;
	@UiField
	Label lblResponse;

	interface Binder extends UiBinder<Widget, HMStaffForAddProject> {
	}

	private final UsersServiceAsync userService = GWT
			.create(UsersService.class);
	private final ProjectStaffServiceAsync projectStaffService = GWT
			.create(ProjectStaffService.class);
	private final SpecializeServiceAsync specService = GWT
			.create(SpecializeService.class);
	private final ProjectServiceAsync projectList = GWT
			.create(ProjectService.class);
	private final MultiSelectionModel<UsersDTO> selectionModel = new MultiSelectionModel<UsersDTO>();
	private final MultiSelectionModel<UsersDTO> selectionModelAdd = new MultiSelectionModel<UsersDTO>();
	// Danh sach NV chua tham gia vao du an nao - duoc lay tu DB
	private final List<UsersDTO> listUser = new ArrayList<UsersDTO>();
	// Danh sach NV duoc chon de add - move
	private final List<UsersDTO> listChoosed = new ArrayList<UsersDTO>();
	private final List<SpecializeDTO> specializeDTOList = new ArrayList<SpecializeDTO>();
	TaskRequireDTO currProjectTaskDTO = new TaskRequireDTO();
	ProjectDTO crrProjDTO = new ProjectDTO();

	public void initForm() {
		initWidget(binder.createAndBindUi(this));
		// Date date = new Date();
		txtFromDate.setFormat(new DateBox.DefaultFormat(DateTimeFormat
				.getFormat("dd/MM/yyyy")));
		txtToDate.setFormat(new DateBox.DefaultFormat(DateTimeFormat
				.getFormat("dd/MM/yyyy")));
		// txtFromDate.setValue(date);
		initCbSpecialize();
		initCbExperient();
		initTable();
		initTableResult();
	}

	public HMStaffForAddProject() {
		initForm();
		initCbProject();
		lblNumEmp.setVisible(false);
		lblResponse.setVisible(false);
		lblNumOfResponse.setVisible(false);
	}

	public HMStaffForAddProject(TaskRequireDTO taskRequireDTO,
			ProjectDTO projectDTO) {
		this.currProjectTaskDTO = taskRequireDTO;
		this.crrProjDTO = projectDTO;
		initForm();
		initCbProject(currProjectTaskDTO.getProjectId());
		// if(taskRequireDTO.getSpecializeId()!=null){
		txtFromDate.setValue(new Date(taskRequireDTO.getStartDate()));
		txtToDate.setValue(new Date(taskRequireDTO.getDueDate()));
		// }
		// else{
		// txtFromDate.setValue(new Date());
		// txtToDate.setValue(new Date(taskRequireDTO.getDueDate()));
		// }
		lblNumOfEmp.setText(taskRequireDTO.getNumOfEmp() + " (Người)");
		
		// txtFromDate.setEnabled(false);
		// txtToDate.setEnabled(false);
		try {
			int exp = taskRequireDTO.getExperience();
			cbExperient.setSelectedIndex(exp);
			cbSpecialize.setEnabled(false);
			cbExperient.setEnabled(false);
		} catch (Exception ex) {
		}
	}

	// Khoi tao combo chuyen mon
	public void initCbSpecialize() {
		specService.getSpecList(new AsyncCallback<List<SpecializeDTO>>() {
			@Override
			public void onSuccess(List<SpecializeDTO> result) {
				cbSpecialize.addItem("---Tất cả---", "0");
				for (SpecializeDTO specializeDTO : result) {
					specializeDTOList.add(specializeDTO);
					cbSpecialize.addItem(specializeDTO.getSpecializeName(),
							specializeDTO.getId());
				}
				if (currProjectTaskDTO.getSpecializeId() != null) {
					int index = 0;
					for (int i = 0; i < specializeDTOList.size(); i++) {
						if (currProjectTaskDTO.getSpecializeId()
								.equalsIgnoreCase(
										specializeDTOList.get(i).getId())) {
							index = i;
						}
					}
					cbSpecialize.setSelectedIndex(index + 1);
					lblNumOfResponse
					.setText(currProjectTaskDTO.getNumOfResponse() + " (Người)");
				} else {
					int index = 0;
					cbSpecialize.setSelectedIndex(index + 3);
					lblNumOfResponse.setText(crrProjDTO.getNumOfOPTRep()+"");
				}
			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});
		cbSpecialize.setSelectedIndex(0);
	}

	// Khoi tao combo kinh nghiem
	public void initCbExperient() {
		cbExperient.addItem("---Tất cả---", "0");
		cbExperient.addItem("< 1 năm", "1");
		cbExperient.addItem("1-3 năm", "2");
		cbExperient.addItem("3-5 năm", "3");
		cbExperient.addItem("5-10 năm", "4");
		cbExperient.addItem("> 10 năm", "5");
		cbExperient.setSelectedIndex(0);
	}

	// Khoi tao combo Danh muc du an
	public void initCbProject() {
		projectList.getProjectList(new AsyncCallback<List<ProjectDTO>>() {
			@Override
			public void onSuccess(List<ProjectDTO> result) {
				cboProject.addItem("---Chọn---", "0");
				for (ProjectDTO projectDTO : result) {
					cboProject.addItem(projectDTO.getProjectName(), projectDTO
							.getProjectID());
				}
			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});
		cboProject.setSelectedIndex(0);
	}

	// Set gia tri cho combobox theo projectId da truyen vao
	public void initCbProject(String _projectId) {
		projectList.getProjectList(new AsyncCallback<List<ProjectDTO>>() {
			@Override
			public void onSuccess(List<ProjectDTO> result) {
				int index = 0;
				cboProject.addItem("---Chọn---", "0");
				for (int i = 0; i < result.size(); i++) {
					cboProject.addItem(result.get(i).getProjectName(), result
							.get(i).getProjectID());
					if (result.get(i).getProjectID().equals(
							currProjectTaskDTO.getProjectId())) {
						index = i + 1;
					}
				}
				cboProject.setSelectedIndex(index);
			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});
		// cboProject.setSelectedIndex(0);
		cboProject.setEnabled(false);
	}

	// Cell table search
	public void initTable() {
		// Check box
		ctbStaffList
				.setSelectionModel(selectionModel, DefaultSelectionEventManager
						.<UsersDTO> createCheckboxManager());
		Column<UsersDTO, Boolean> checkColumn = new Column<UsersDTO, Boolean>(
				new CheckboxCell(true, false)) {
			@Override
			public Boolean getValue(UsersDTO object) {
				// Get the value from the selection model.
				return selectionModel.isSelected(object);
			}
		};

		// Ma nhan vien
		TextColumn<UsersDTO> userCodeColumn = new TextColumn<UsersDTO>() {
			@Override
			public String getValue(UsersDTO userCode) {
				return userCode.getUserCode();
			}
		};

		// Ten nhan vien
		TextColumn<UsersDTO> userNameColumn = new TextColumn<UsersDTO>() {
			@Override
			public String getValue(UsersDTO user) {
				return user.getFullName();
			}
		};

		// Chuyen mon
		TextColumn<UsersDTO> specializeColumn = new TextColumn<UsersDTO>() {
			@Override
			public String getValue(UsersDTO user) {
				return user.getSpecializeName();
			}
		};

		// Kinh nghiem
		TextColumn<UsersDTO> experientColumn = new TextColumn<UsersDTO>() {
			@Override
			public String getValue(UsersDTO user) {
				int exp = user.getExperient();
				String result = "";
				if (exp == 1) {
					result += " < 1 năm";
				} else if (exp == 2) {
					result += "1-3 năm";
				} else if (exp == 3) {
					result += "3-5 năm";
				} else if (exp == 4) {
					result += "5-10 năm";
				} else if (exp == 5) {
					result += " > 10 năm";
				}
				return result;
			}
		};

		ctbStaffList.setWidth("68%", true);
		ctbStaffList.setPageSize(5);
		ctbStaffList.setPageStart(0);
		ctbStaffList.setStyleName("ttable");

		ctbStaffList.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant(""));
		ctbStaffList.setColumnWidth(checkColumn, 30, Unit.PX);
		checkColumn.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);

		ctbStaffList.addColumn(userCodeColumn, "Mã nhân viên");
		ctbStaffList.setColumnWidth(userCodeColumn, 15.0, Unit.PCT);

		ctbStaffList.addColumn(userNameColumn, "Tên nhân viên");
		ctbStaffList.setColumnWidth(userNameColumn, 20.0, Unit.PCT);

		ctbStaffList.addColumn(specializeColumn, "Chuyên môn");
		ctbStaffList.setColumnWidth(specializeColumn, 15.0, Unit.PCT);

		ctbStaffList.addColumn(experientColumn, "Kinh nghiệm");
		ctbStaffList.setColumnWidth(experientColumn, 15.0, Unit.PCT);

	}

	private void initTableResult() {
		// Check box
		ctbResult
				.setSelectionModel(selectionModelAdd,
						DefaultSelectionEventManager
								.<UsersDTO> createCheckboxManager());
		Column<UsersDTO, Boolean> checkColumn = new Column<UsersDTO, Boolean>(
				new CheckboxCell(true, false)) {
			@Override
			public Boolean getValue(UsersDTO object) {
				// Get the value from the selection model.
				return selectionModelAdd.isSelected(object);
			}
		};

		// Ma nhan vien
		TextColumn<UsersDTO> userCodeColumn = new TextColumn<UsersDTO>() {
			@Override
			public String getValue(UsersDTO userCode) {
				return userCode.getUserCode();
			}
		};

		// Ten nhan vien
		TextColumn<UsersDTO> userNameColumn = new TextColumn<UsersDTO>() {
			@Override
			public String getValue(UsersDTO user) {
				return user.getFullName();
			}
		};

		// Chuyen mon
		TextColumn<UsersDTO> specializeColumn = new TextColumn<UsersDTO>() {
			@Override
			public String getValue(UsersDTO user) {
				return user.getSpecializeName();
			}
		};

		// Kinh nghiem
		TextColumn<UsersDTO> experientColumn = new TextColumn<UsersDTO>() {
			@Override
			public String getValue(UsersDTO user) {
				int exp = user.getExperient();
				String result = "";
				if (exp == 1) {
					result += " < 1 năm";
				} else if (exp == 2) {
					result += "1-3 năm";
				} else if (exp == 3) {
					result += "3-5 năm";
				} else if (exp == 4) {
					result += "5-10 năm";
				} else if (exp == 5) {
					result += " > 10 năm";
				}
				return result;
			}
		};

		ctbResult.setWidth("68%", true);
		ctbResult.setPageSize(5);
		ctbResult.setPageStart(1);
		ctbResult.setStyleName("ttable");

		ctbResult.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant(""));
		ctbResult.setColumnWidth(checkColumn, 30, Unit.PX);
		checkColumn.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);

		ctbResult.addColumn(userCodeColumn, "Mã nhân viên");
		ctbResult.setColumnWidth(userCodeColumn, 15.0, Unit.PCT);

		ctbResult.addColumn(userNameColumn, "Tên nhân viên");
		ctbResult.setColumnWidth(userNameColumn, 20.0, Unit.PCT);

		ctbResult.addColumn(specializeColumn, "Chuyên môn");
		ctbResult.setColumnWidth(specializeColumn, 15.0, Unit.PCT);

		ctbResult.addColumn(experientColumn, "Kinh nghiệm");
		ctbResult.setColumnWidth(experientColumn, 15.0, Unit.PCT);
	}

	protected void loadData(List<UsersDTO> userList) {
		ListDataProvider<UsersDTO> dataProvider = new ListDataProvider<UsersDTO>();

		// Add the data to the data provider, which automatically pushes it to
		// the widget
		List<UsersDTO> list = dataProvider.getList();
		for (UsersDTO user : userList) {
			list.add(user);
		}
		pagerListSearch.setDisplay(ctbStaffList);
		// Connect the table to the data provider.
		dataProvider.addDataDisplay(ctbStaffList);
	}

	protected void loadDataAdd(List<UsersDTO> userList) {
		// TODO Auto-generated method stub
		ListDataProvider<UsersDTO> dataProvider = new ListDataProvider<UsersDTO>();

		// Add the data to the data provider, which automatically pushes it to
		// the widget
		List<UsersDTO> listUser = dataProvider.getList();
		for (UsersDTO user : userList) {
			listUser.add(user);
		}
		pagerAddList.setDisplay(ctbResult);
		// Connect the table to the data provider.
		dataProvider.addDataDisplay(ctbResult);
	}

	@UiHandler("btnMove")
	void onBtnMoveClick(ClickEvent event) {
		Set<UsersDTO> listChecked = selectionModel.getSelectedSet();
		if (listChecked.isEmpty()) {
			Window.alert("Bạn chưa chọn nhân viên để thêm vào dự án!");
		} else {
			for (UsersDTO user : listChecked) {
				listChoosed.add(user);
				listUser.remove(user);
			}
			selectionModel.clear();
			// loadData(listUser);
			searchFunction(listUser);
			loadDataAdd(listChoosed);
		}

	}

	@UiHandler("btnRemove")
	void onBtnRemoveClick(ClickEvent event) {
		Set<UsersDTO> listChecked = selectionModelAdd.getSelectedSet();
		if (listChecked.isEmpty()) {
			Window.alert("Bạn chưa chọn nhân viên để chuyển lại!");
		} else {
			List<UsersDTO> listDelete = new ArrayList<UsersDTO>();
			for (UsersDTO user : listChecked) {
				listDelete.add(user);
			}
			if (listDelete != null) {
				for (int i = 0; i < listDelete.size(); i++) {
					for (int j = 0; j < listChoosed.size(); j++) {
						if (listDelete.get(i).getUserCode().equals(
								listChoosed.get(j).getUserCode())) {
							listUser.add(listDelete.get(i));
							listChoosed.remove(j);
						}
					}
				}
				selectionModelAdd.clear();
			} else {
				Window.alert("Bạn chưa chọn để xóa!");
			}
			// loadData(listUser);
			searchFunction(listUser);
			loadDataAdd(listChoosed);
		}
	}

	@UiHandler("btnCancel")
	void onBtnCancelClick(ClickEvent event) {
		// Huy bo Them nhan vien moi
		HMEntrypoint.get().HMHumanReqDetail(crrProjDTO);
	}

	@UiHandler("btnAdd")
	void onBtnAddClick(ClickEvent event) {
		// Se kiem soat dieu kien truoc khi add nhan vien tai day
		if (cboProject.getValue(cboProject.getSelectedIndex()).equals("0")) {
			Window.alert("Bạn phải chọn Dự án!");
		} else {
			final String projectId = cboProject.getValue(cboProject
					.getSelectedIndex());
			// Them moi nhan vien vao du an - load lai thong tin bang Nhan vien
			// cua
			// du an
			// truoc khi load phai cap nhat lai thong tin projectId trong thong
			// tin
			// cua nhan vien da duoc add
			projectList.getProjectById(projectId,
					new AsyncCallback<ProjectDTO>() {
						@Override
						public void onSuccess(ProjectDTO projectDTO) {
							if (listChoosed.size() > 0) {
								// Thiet lap thoi gian nhan vien tham gia du an
								// - theo thoi gian da tim kiem
								projectDTO.setStartDate(txtFromDate.getValue()
										.getTime());
								projectDTO.setDueDate(txtToDate.getValue()
										.getTime());
								projectStaffService.addStaffList(listChoosed,
										projectDTO, currProjectTaskDTO,
										new AsyncCallback<Void>() {

											@Override
											public void onSuccess(Void result) {
												Window
														.alert("Thêm nhân viên vào dự án thành công!");
											}

											@Override
											public void onFailure(
													Throwable caught) {
												Window
														.alert("Thêm nhân viên vào dự án thất bại!");
											}
										});
								HMEntrypoint.get().HMProjectStaffList(
										crrProjDTO);
							} else {
								Window
										.alert("Bạn chưa chọn nhân viên để thêm vào dự án!");
							}
						}

						@Override
						public void onFailure(Throwable caught) {
							Window
									.alert("Có lỗi trong quá trình lấy thông tin dự án!");
						}
					});
		}

	}

	@UiHandler("btnSearch")
	void onBtnSearchClick(ClickEvent event) {
		if (!listUser.isEmpty()) {
			listUser.removeAll(listUser);
		}
		initValue();
		selectionModel.clear();
	}

	// Ham khoi tao gia tri cho bang
	public void initValue() {
		Date toDay = new Date();
		Date _fromDate = txtFromDate.getValue();
		Date _toDate = txtToDate.getValue();
		// Validate ngay thang
		if (_fromDate == null || _fromDate.equals("")) {
			Window.alert("Bạn phải nhập thời gian Từ ngày!");
		} else if (_toDate == null || _toDate.equals("")) {
			Window.alert("Bạn phải nhập thời gian Đến ngày!");
		} else {
			// int check1 = CommonFuntion.compareWithTwoDay(_fromDate, toDay);
			int check2 = CommonFuntion.compareWithTwoDay(_fromDate, _toDate);
			// if(check1 == 1) {
			// Window.alert("Bạn phải nhập thời gian Từ ngày lớn hơn hoặc bằng ngày hiện tại!");
			// } else
			if (check2 == 2) {
				Window
						.alert("Bạn phải nhập thời gian Từ ngày nhỏ hơn hoặc bằng thời gian Đến ngày!");
			} else {
				searchPJSDTOList(_fromDate, _toDate);
			}
		}
	}

	// Ham tim kiem NV dang tham gia vao du an trong 1 khoang thoi gian nhat
	// dinh
	// tra ve mot list ProjectStaffDTO (userId) co startDate hoac finishDate
	// trong khoang do
	public void searchPJSDTOList(Date fromDate, Date toDate) {
		Long _fromDate = CommonFuntion.removeTime(fromDate);
		Long _toDate = CommonFuntion.removeTime(toDate);

		projectStaffService.searchPJWithTime(_fromDate, _toDate,
				new AsyncCallback<List<ProjectStaffDTO>>() {

					@Override
					public void onSuccess(List<ProjectStaffDTO> pjStaffResult) {
						userService.getUserListExceptById(pjStaffResult,
								new AsyncCallback<List<UsersDTO>>() {

									@Override
									public void onSuccess(
											List<UsersDTO> userResult) {
										for (UsersDTO user : userResult) {
											if (!userResult.isEmpty()) {
												if (user.getUserGroup() == null) {
													user.setUserGroup(0L);
												}
												if (user.getUserGroup() == 3L
														|| user.getUserGroup() == 5L) { // Chi
																						// tim
																						// ra
																						// 2
																						// doi
																						// tuong
																						// sau:
																						// 3
																						// -
																						// Giam
																						// sat;
																						// 5
																						// -
																						// Nhan
																						// vien
													int check = 0;
													if (!listChoosed.isEmpty()) {
														int size = listChoosed
																.size();
														for (int i = 0; i < size; i++) {
															if (user
																	.getUserId()
																	.equals(
																			listChoosed
																					.get(
																							i)
																					.getUserId())) {
																check++;
															}
														}
													}
													if (check == 0) {
														listUser.add(user);
													}
												}
											}
										}
										searchFunction(listUser);
									}

									@Override
									public void onFailure(Throwable caught) {
									}
								});
					}

					@Override
					public void onFailure(Throwable caught) {
					}
				});
	}

	// Ham tim kiem
	public void searchFunction(List<UsersDTO> userList) {
		String _name = "";
		String _code = "";
		String _specializeName = "";
		for (int i = 0; i < userList.size(); i++) {
			if (userList.get(i).getUserName() == null
					|| userList.get(i).getUserName().equals("")) {
				userList.remove(i);
			}
		}
		if (txtName.getText().trim() != null
				&& !txtName.getText().trim().equals("")) {
			_name += txtName.getText().trim();
		}

		if (txtCode.getText().trim() != null
				&& !txtCode.getText().trim().equals("")) {
			_code += txtCode.getText().trim();
		}

		if (cbSpecialize.getValue(cbSpecialize.getSelectedIndex()) != null
				&& !cbSpecialize.getValue(cbSpecialize.getSelectedIndex())
						.equals("0")) {
			_specializeName += cbSpecialize.getValue(
					cbSpecialize.getSelectedIndex()).trim();
		}

		int experient = -2;
		if (cbExperient.getValue(cbExperient.getSelectedIndex()) != null) {
			experient = Integer.parseInt(cbExperient.getValue(cbExperient
					.getSelectedIndex()));
		}

		Boolean condition = true;

		// Viec tim kiem chi dua tren List da duoc lay ra tu DB - listUser
		List<UsersDTO> temple = new ArrayList<UsersDTO>();

		if ((txtName.getText().trim() == null || txtName.getText().trim()
				.equals(""))
				&& (txtCode.getText().trim() == null || txtCode.getText()
						.trim().equals(""))
				&& (cbSpecialize.getValue(cbSpecialize.getSelectedIndex())
						.equalsIgnoreCase("0"))
				&& (cbExperient.getValue(cbExperient.getSelectedIndex())
						.equalsIgnoreCase("0"))) {
			loadData(userList);
		} else {
			for (int i = 0; i < userList.size(); i++) {
				if (txtName.getText().trim() != null
						&& !txtName.getText().trim().equals("")) {
					condition = condition
							&& (listUser.get(i).getFullName().contains(_name));
				}
				if (txtCode.getText().trim() != null
						&& !txtCode.getText().trim().equals("")) {
					condition = condition
							&& (listUser.get(i).getUserCode().contains(_code));
				}
				if (listUser.get(i).getSpecializeID() == null) {
					listUser.get(i).setSpecializeID("0");
				}
				if ((cbSpecialize.getValue(cbSpecialize.getSelectedIndex()) != null)
						&& (!cbSpecialize.getValue(
								cbSpecialize.getSelectedIndex())
								.equalsIgnoreCase("0"))) {
					condition = condition
							&& (listUser.get(i).getSpecializeID()
									.equals(_specializeName));
				}
				if ((cbExperient.getValue(cbExperient.getSelectedIndex()) != null)
						&& (!cbExperient.getValue(
								cbExperient.getSelectedIndex())
								.equalsIgnoreCase("0"))) {
					condition = condition
							&& (listUser.get(i).getExperient() == experient);
				}
				if (condition) {
					temple.add(listUser.get(i));
				}
				condition = true;
			}
			if (temple != null) {
				loadData(temple);
			} else {
				Window.alert("Không tìm thấy dữ liệu");
				loadData(temple);
			}
		}
	}
}
